package com.kaifamiao.type;

import com.kaifamiao.enums.Gender;
import org.apache.ibatis.type.*;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Alias("GenderTypeHandler")
@MappedTypes(Gender.class)
public class GenderTypeHandler extends BaseTypeHandler<Gender> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int index, Gender gender, JdbcType jdbcType) throws SQLException {
        // 暂时将 Gender 枚举中枚举常量的索引值保存到数据库中
        ps.setInt( index, gender.ordinal() );
    }

    @Override
    public Gender getNullableResult(ResultSet rs, String label) throws SQLException {
        int ordinal = rs.getInt( label );
        return Gender.values()[ ordinal ];
    }

    @Override
    public Gender getNullableResult(ResultSet rs, int index) throws SQLException {
        int ordinal = rs.getInt( index );
        return Gender.values()[ ordinal ];
    }

    @Override
    public Gender getNullableResult(CallableStatement cs, int index) throws SQLException {
        int ordinal = cs.getInt( index );
        return Gender.values()[ ordinal ];
    }

}
